/*
 * Copyright (C) 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.zxing.scanner.camera;

import com.mylhyl.zxing.scanner.ScannerOptions;
import com.mylhyl.zxing.scanner.camera.open.CameraFacing;
import com.mylhyl.zxing.scanner.camera.open.OpenCamera;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.media.camera.CameraKit;
import ohos.media.camera.device.Camera;
import ohos.media.camera.device.CameraAbility;
import ohos.media.camera.device.FrameConfig;
import ohos.media.camera.params.Metadata;

import static ohos.media.camera.device.Camera.FrameConfigType.FRAME_CONFIG_PREVIEW;


/**
 * 相机辅助类，主要用于设置相机的各类参数
 */
@SuppressWarnings("deprecation")
final class CameraConfigurationManager {

    private final Context context;
    private int cwRotationFromDisplayToCamera;
    // 屏幕分辨率
    private Point screenResolution;
    // 相机分辨率
    private Point cameraResolution;
    private Point bestPreviewSize;
    private ScannerOptions scannerOptions;

    CameraConfigurationManager(Context context, ScannerOptions scannerOptions) {
        this.context = context;
        this.scannerOptions = scannerOptions;
    }

    /**
     * 计算了屏幕分辨率和当前最适合的相机像素
     */
    void initFromCameraParameters(OpenCamera camera) {
        CameraAbility parameters = camera.getCameraKit().getCameraAbility(camera.getCameraId());
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();

        int cwRotationFromNaturalToDisplay = 0;

        int cwRotationFromNaturalToCamera = camera.getOrientation();

        if (camera.getFacing() == CameraFacing.FRONT) {
            cwRotationFromNaturalToCamera = (360 - cwRotationFromNaturalToCamera) % 360;
        }

        cwRotationFromDisplayToCamera =
                (360 + cwRotationFromNaturalToCamera - cwRotationFromNaturalToDisplay) % 360;

        Point theScreenResolution = new Point();
        display.getSize(theScreenResolution);
        screenResolution = theScreenResolution;

        Point screenResolutionForCamera = new Point(screenResolution.getPointX(), screenResolution.getPointY());
        if (screenResolution.getPointX() < screenResolution.getPointY()) {
            screenResolutionForCamera = new Point(screenResolution.getPointY(), screenResolution.getPointX());
        }
        cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolutionForCamera);
        bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolutionForCamera);

    }

    /**
     * 读取配置设置相机的对焦模式、闪光灯模式等等
     *
     * @param camera
     * @param safeMode
     */
    void setDesiredCameraParameters(OpenCamera camera, boolean safeMode, boolean invertScan) {

        CameraKit cameraKit = camera.getCameraKit();
        Camera theCamera = camera.getCamera();
        //获取指定逻辑相机或物理相机的静态能力
        CameraAbility cameraAbility = cameraKit.getCameraAbility(camera.getCameraId());
        //获取预览配置模板
        FrameConfig.Builder frameConfigBuilder = camera.getCamera().getFrameConfigBuilder(FRAME_CONFIG_PREVIEW);
        if (cameraAbility == null) {
            return;
        }

        if (safeMode) {
        }

        // 自动对焦
        boolean autoFocus = true;
        // 持续对焦
        boolean disableContinuousFocus = true;
        CameraConfigurationUtils.setFocus(cameraAbility, frameConfigBuilder, autoFocus, disableContinuousFocus, safeMode);

        theCamera.triggerLoopingCapture(frameConfigBuilder.build());
    }

    Point getCameraResolution() {
        return cameraResolution;
    }

    Point getScreenResolution() {
        return screenResolution;
    }

    boolean getTorchState(Camera camera) {
        if (camera != null) {
            //获取预览配置模板
            FrameConfig.Builder frameConfigBuilder = camera.getFrameConfigBuilder(FRAME_CONFIG_PREVIEW);
            if (frameConfigBuilder != null) {
                int flashMode = frameConfigBuilder.getFlashMode();
                return
                        Metadata.FlashMode.FLASH_OPEN == flashMode ||
                                Metadata.FlashMode.FLASH_ALWAYS_OPEN == flashMode;
            }
        }
        return false;
    }

    void setTorch(CameraAbility cameraAbility, FrameConfig.Builder builder, boolean newSetting) {

        doSetTorch(cameraAbility, builder, newSetting, false);
    }

    private void doSetTorch(CameraAbility cameraAbility, FrameConfig.Builder builder, boolean newSetting, boolean safeMode) {

    }

}
